package com.pengtu.web.app;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Organization;
import com.pengtu.entity.app.Role;
import com.pengtu.entity.app.User;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author liuyang
 * 
 */
@Namespace("/app")
@Results({
		@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
		@Result(name = BaseActionSupport.LOGIN, type = "redirect", location = "${ctx}/project/sign/design.action?projectId=${projectId}&contractKind=${typeId}"),
		@Result(name = BaseActionSupport.NONE, type = "redirect", location = "${ctx}/j_spring_security_check?j_username=${user.userName}&j_password=${user.password}") })
public class OrganizationAction extends BaseActionSupport<Organization> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private Organization entity;
	private List<Organization> allOrganizationList;
	private Page<Organization> page = new Page<Organization>(10);// 每页5条记录
	private String typeId;
	private String projectId;

	@Override
	public Organization getModel() {
		return entity;
	}

	public String list() throws Exception {
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (filters == null) {
			allOrganizationList = accountManager.getOrganization();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchOrganization(page, filters);
		return SUCCESS;
	}

	@Action("/account/api/organization")
	public String apilist() throws Exception {
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());

		// 设置默认排序方式
		if (filters == null) {
			allOrganizationList = accountManager.getOrganization();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchOrganization(page, filters);
		return SUCCESS;
	}

	public String signlist() throws Exception {

		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryfilter.addFilter(new String[] { "type" }, typeId);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchTypeOrganization(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		accountManager.saveOrganization(entity);
		addActionMessage("保存单位成功");
		if (StringUtils.isNotEmpty(projectId))
			return LOGIN;
		return RELOAD;
	}

	/**
	 * 验证机构代码是否重复
	 */
	public String checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (accountManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	/**
	 * 验证单位名是否重复
	 * @throws Exception 
	 */
	public String checkOrgName() throws Exception {
		// TODO Auto-generated method stub
		String orgName = new String(Struts2Utils.getParameter("orgName").getBytes("iso8859-1"),"UTF-8");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (accountManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	/**
	 * 注册
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		accountManager.saveOrganization(entity);
		User user = new User();
		Set<Role> roleList = new HashSet<Role>();
		roleList.add(accountManager.findRole("普通用户"));
		user.setUserName(Struts2Utils.getParameter("userName"));
		user.setPassword(Struts2Utils.getParameter("password"));
		user.setOrganization(entity);
		user.setEmail(Struts2Utils.getParameter("userEmail"));
		user.setName(Struts2Utils.getParameter("name"));
		user.setRoleList(roleList);
		user.setExpired("0");
		user.setLocked("0");
		user.setCredexpired("0");
		user.setEnable("0");
		accountManager.saveUser(user);
		addActionMessage("保存成功");
		return NONE;
	}
	
	/**
	 * 根据单位名称查询
	 * @return
	 * @throws Exception
	 */
	public String search()throws Exception {
		String orgName = new String(Struts2Utils.getParameter("org").getBytes("iso8859-1"),"utf-8");
		List<Organization> orgList = accountManager.getAll(orgName);
		Struts2Utils.renderJson(orgList);
		return null;
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.deleteOrganization(id);
		addActionMessage("删除单位信息");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getOrganization(id);
		} else {
			entity = new Organization();
		}
	}
	
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getOrganization(id);
		} else {
			entity = new Organization();
		}
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}

	public List<Organization> getAllOrganizationList() {
		return allOrganizationList;
	}

	public Page<Organization> getPage() {
		return page;
	}

	public void setPage(Page<Organization> page) {
		this.page = page;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

}
